//
//  WOFilesViewController.m
//
//  Created by Olof Millberg on 2010-03-25.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WOFile.h"
#import "WOFilesViewController.h"
#import "WOPrototypeAppDelegate.h"
#import "DocumentViewController.h"

#import "WOFileSection.h"

#import "FileCell.h"

@implementation WOFilesViewController

#pragma mark Initialization Methods

/* Initialize */
- (id) init
{
	if (self = [super initWithStyle:UITableViewStylePlain])
	{
		//Set title.
		self.title = @"Filer";
		//Create, set and release the tab bar item.
		UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Filer" image:[UIImage imageNamed:@"tabbar_files.png"] tag:4];
		self.tabBarItem = theItem;
		[theItem release];
	}
	return self;
}

/* The view has appeard */
- (void) viewDidAppear:(BOOL)animated
{
	self.tabBarController.title = self.title;
	[[NSUserDefaults standardUserDefaults] setInteger:3 forKey:@"currentTab"];
}

/* Create the actual view */
- (void) loadView
{
	[super loadView];
	
	self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
	self.tableView.separatorColor = [UIColor groupTableViewBackgroundColor];
	
	self.sections = [NSMutableArray array];
	[self.sections addObject:[WOFileSection fileSectionWithList:workOrder.attachedFiles]];
}

#pragma mark -

#pragma mark UITableViewMethods Methods

/* Sets the number of sections in the table view */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

/* Sets the number of rows in the table view */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
	return workOrder.attachedFiles.count;
}

/* Sets the disclosure indicator */
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView 
		 accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 
{
	return UITableViewCellAccessoryDisclosureIndicator;
}

/* Creating the cells in the table view */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    	
	
	if ([cell isKindOfClass:[FileCell class]])
	{
		WOFile *currentFile = [workOrder.attachedFiles objectAtIndex:indexPath.row];
		((FileCell*)cell).file = currentFile;
	}
	
    return cell;
}

/* A cell is selected */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	DocumentViewController *documentViewController = [[DocumentViewController alloc] 
													  initWithNibName:@"DocumentViewController" bundle:nil];
	
	WOFile *selectedFile = [workOrder.attachedFiles objectAtIndex:indexPath.row];
	
	documentViewController.documentURL = selectedFile.fileURLPath;
	documentViewController.titleFileName = selectedFile.fileName;
	
	WOPrototypeAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	[appDelegate.navigationController pushViewController:documentViewController animated:YES];
	
	[documentViewController release];
}

#pragma mark -

@end